//
//  HMEmoticonButton.swift
//  Weibo02
//
//  Created by teacher on 16/4/28.
//  Copyright © 2016年 teacher. All rights reserved.
//

import UIKit

class HMEmoticonButton: UIButton {

    // 当前表情按钮对应的表情模型
    var emoticon: HMEmoticon? {
        didSet{
            guard let _ = emoticon else {
                return
            }
            // 判断是 emoji 表情还是图片表情
            if emoticon?.type == "1" {
                // 是 emoji 表情
                self.setTitle((emoticon!.code! as NSString).emoji(), forState: UIControlState.Normal)
                // 因为会复用，所以设置emoji表情的时候，需要将图片给干掉
                self.setImage(nil, forState: UIControlState.Normal)
            }else{
                // 设置图片表情数据
                let image = UIImage(named: "\(emoticon?.folderName ?? "")/\(emoticon?.png ?? "")", inBundle: HMEmoticonKeyBoardViewModel.sharedViewModel.emoticonBundle, compatibleWithTraitCollection: nil)
                self.setImage(image, forState: UIControlState.Normal)
                self.setTitle(nil, forState: UIControlState.Normal)
            }
        }
    }
}
